package javaMultiThread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 
 *
 */
/**
 * ReentrantLock lock持有对象监听
 * 
 *
 */
public class Thread4 {

	public static void main(String[] args) {
		/*Thread4A t4a=new Thread4A();
		t4a.start();*/
		Thread4C t4c=new Thread4C();
		t4c.t1();
	}

}
class Thread4A extends Thread{
	private Lock rlock=new ReentrantLock();
	@Override
	public void run() {
		super.run();
		rlock.lock();
		rlock.newCondition();
		for(int i=0;i<100;i++){
			System.out.println(i);
		}
		rlock.unlock();
	}
}
class Thread4B {
	private Lock rlock=new ReentrantLock();//(分为公平锁和非公平锁构造方法true为公平锁)
	private Condition condition=rlock.newCondition();
	public void await(){
		try {
			rlock.lock();
			condition.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			rlock.unlock();
		}
	}
	public void signal(){
		rlock.lock();
		condition.signal();
		rlock.unlock();
	}
}
class Thread4C{
	private ReentrantLock rlock=new ReentrantLock();
	private Condition condition=rlock.newCondition();
	public void t1(){
		rlock.lock();		
		System.out.println("当前线程持有rlock锁的个数:"+rlock.getHoldCount());
		System.out.println("没有await的线程数:"+rlock.getQueueLength());
		t2();
		rlock.unlock();
	}
	public void t2(){
		rlock.lock();		
		System.out.println("当前线程持有rlock锁的个数:"+rlock.getHoldCount());
		System.out.println("没有await的线程数:"+rlock.getQueueLength());
		rlock.unlock();
	}
	public void t3(){
		rlock.lock();
		try {
			condition.await();
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
	}
}
class Thread4D{
	ReentrantReadWriteLock rrwl=new ReentrantReadWriteLock();
	public void t1(){
		//只有读读可以异步执行,其他都是同步执行 
		rrwl.readLock().lock();
		rrwl.readLock().unlock();
	}
	public void t2(){
		rrwl.writeLock().lock();
		rrwl.writeLock().unlock();
	}
	
}
